﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Diagnostics;

namespace Midapex.SmartOrm.Tools.Wizard
{
    public partial class OrmApplicationControl : UserControl
    {
        public OrmApplicationControl()
        {
            InitializeComponent();
        }

        public string ExePath
        {
            get { return tbApp.Text; }
        }

        void UpdateListView()
        {
            listView1.Items.Clear();

            foreach (Assembly ass in OrmWizard.OrmAssemblies)
            {
                listView1.Items.Add(new ListViewItem(
                            new string[] { ass.GetName().Name, ass.ImageRuntimeVersion}));
            }
        }

        private void buttonSelectApp_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonAddDll_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialogAddAssembly.ShowDialog();
            if(result == DialogResult.OK)
            {
                string file = this.openAppDialog.FileName;
                Assembly assembly = Assembly.LoadFile(file);
                if(EntityClassesFactory.IsEntityClassesAssembly(assembly))
                {
                    OrmWizard.OrmAssemblies.Add(assembly);
                }
                UpdateListView();
            }
        }

        private void OrmApplicationControl_Load(object sender, EventArgs e)
        {

        }

        private void linkLabelSelectApp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if(radioButtonAspNet.Checked)
            {
                openWebConfigDialog.RestoreDirectory = true;
                openWebConfigDialog.InitialDirectory = GetDir(GetDir(Application.ExecutablePath));

                DialogResult dialogResult = openWebConfigDialog.ShowDialog();
                
                if (dialogResult == DialogResult.OK)
                {
                    tbApp.Text = openWebConfigDialog.FileName;
                    ShowApplicationInfo();
                    //获得配置文件
                    ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
                    configFile.ExeConfigFilename = tbApp.Text;
                    Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
                        
                    Debug.Assert(configuration != null);

                    OrmWizard.Configuration = configuration;
                }
                return;
            }

            openAppDialog.RestoreDirectory = true;
            openAppDialog.InitialDirectory = GetDir(Application.ExecutablePath);

            DialogResult result = this.openAppDialog.ShowDialog();
            
            if (result == DialogResult.OK)
            {
                tbApp.Text = this.openAppDialog.FileName;

                ShowApplicationInfo();
                Assembly app = Assembly.LoadFile(tbApp.Text);
                if (EntityClassesFactory.IsEntityClassesAssembly(app))
                {
                    OrmWizard.OrmAssemblies.Add(app);
                }

                //获得配置文件
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ExePath);
                Debug.Assert(configuration != null);

                OrmWizard.Configuration = configuration;
            }
        }

        string GetDir(string path)
        {
            int index = path.LastIndexOf("\\");
            return path.Substring(0,index);
        }
        private void ShowApplicationInfo()
        {
            OrmWizard.OrmAssemblies.Clear();

            string dir = GetDir(tbApp.Text);

            //获得该ORM应用程序关联的ORM引用组件
            string[] dllFiles = Directory.GetFiles(dir, "*.dll",
                SearchOption.TopDirectoryOnly);

            foreach (string dllFileName in dllFiles)
            {
                Assembly assembly = Assembly.LoadFile(dllFileName);
                if (EntityClassesFactory.IsEntityClassesAssembly(assembly))
                {
                    OrmWizard.OrmAssemblies.Add(assembly);
                }
            }

            UpdateListView();
        }
    }
}
